﻿using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using DataCenter.Interface.Model;

namespace DataCenter.Utility
{
    public partial class OpenAssemblyForm : Form
    {
        private OpenAssemblyForm()
        {
            InitializeComponent();
        }
        public OpenAssemblyForm(Type attributeType)
            : this()
        {
            this.attributeType = attributeType;
        }
        private Type attributeType;
        private Assembly assembly;
        private ComponentEntity entity;
        public ComponentEntity Entity
        {
            get { return entity; }
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                lstvComponent.Items.Clear();
                if (!File.Exists(openFileDialog1.FileName))
                    MessageBox.Show("找不到文件\"" + openFileDialog1.FileName + "\"");

                try
                {
                    assembly = Assembly.LoadFile(openFileDialog1.FileName);
                    foreach (Type type in assembly.GetTypes())
                    {
                        if (Attribute.IsDefined(type, attributeType))
                        {
                            object[] attributes = type.GetCustomAttributes(attributeType, false);
                            ComponentAttribute attr = (ComponentAttribute)attributes[0];

                            ListViewItem item = new ListViewItem();
                            if (string.IsNullOrEmpty(attr.Name)) item.Text = type.FullName;
                            else item.Text = attr.Name;
                            if (attr.Version != null) item.SubItems.Add(attr.Version.ToString());
                            if (!string.IsNullOrEmpty(attr.Description)) item.SubItems.Add(attr.Description);
                            item.Tag = type;
                            lstvComponent.Items.Add(item);
                        }
                    }

                    if (lstvComponent.Items.Count > 0)
                    {
                        lstvComponent.Items[0].Selected = true;
                        btnOk.Enabled = true;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("加载程序集发生错误\r\n" + ex.Message, this.Text, 
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }                                
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (lstvComponent.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择一个可用的组件。", this.Text, 
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            Type type = (Type)lstvComponent.SelectedItems[0].Tag;
            entity = (ComponentEntity)assembly.CreateInstance(type.FullName, true);
            entity.AssemblyName = assembly.Location;
            entity.TypeName = type.FullName;

            this.DialogResult = DialogResult.OK;
            this.Close();
        }        
    }
}
